package com.hx.servlet;

import com.hx.dao.impl.BizDaoImpl;
import com.hx.entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String name=request.getParameter("name");
        String password=request.getParameter("password");

        BizDaoImpl biz = new BizDaoImpl();
        boolean flag = biz.validateUser(name,password);
        HttpSession session = request.getSession();
        if(flag){
            Admin admin = new Admin();
            BizDaoImpl bizDao = new BizDaoImpl();
            admin = bizDao.getAdminInfo(name,password);
            session.setAttribute("aid",admin.getAid());
            System.out.println("------>>>"+session.getAttribute("aid"));
            session.setMaxInactiveInterval(6000);
            request.setAttribute("message","登录成功!");
            request.getRequestDispatcher("/success.jsp").forward(request,response);
        }else{
            request.setAttribute("message","账号或者密码错误!");
            request.getRequestDispatcher("/error.jsp").forward(request,response);
        }

        /*if(name.equals("admin") && "123456".equals(password)){
            response.sendRedirect("success.jsp");
        }else{
            System.out.println("登入失败");
            response.sendRedirect("error.jsp");
        }*/

        //判断用户是否提交了账号密码
//        if (StrUtil.isBlankIfStr(name) || StrUtil.isBlankIfStr(password)){
//            request.setAttribute("message","账号或者密码不能为空!");
//            request.getRequestDispatcher("/error.jsp").forward(request,response);
//        }

        //判断账号密码是否正确
//        if (!"admin".equals(name) || !"123456".equals(password)){
//            request.setAttribute("message","账号或者密码错误!");
//            request.getRequestDispatcher("/error.jsp").forward(request,response);
//        }

        //账号密码正确,跳转到成功页面
//        request.setAttribute("message","登录成功!");
//        request.getRequestDispatcher("/success.jsp").forward(request,response);
    }
}
